package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.account.dao.TCoachInfoMapper;
import com.woniu.account.dao.TCoachMapper;
import com.woniu.account.dto.CoachInfoDto;
import com.woniu.account.form.CoachInfoPerfectForm;
import com.woniu.account.form.RealNameAuthenticationForm;
import com.woniu.account.model.TCoach;
import com.woniu.account.model.TCoachInfo;
import com.woniu.account.service.TCoachInfoService;
import com.woniu.core.exception.account.AccountException;
import com.woniu.core.exception.account.AccountExceptionCode;
import com.woniu.core.util.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.woniu.core.exception.account.AccountExceptionCode.MODIFICATION_FAILED;
import static com.woniu.core.exception.account.AccountExceptionCode.NOT_FIND_THIS_USER;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TCoachInfoServiceImpl extends ServiceImpl<TCoachInfoMapper, TCoachInfo> implements TCoachInfoService {
    @Resource
    private TCoachInfoMapper tCoachInfoMapper;
    @Resource
    private TCoachMapper tCoachMapper;
    @Override
    public CoachInfoDto CoachInfoById(Integer coachId) {
        TCoach coach = tCoachMapper.selectById(coachId);
        if (coach.getCoachState()==0) throw new AccountException(AccountExceptionCode.COACH_ACCOUNT_IS_FORZEN_AND_CANT_BE_VIEWED);
        TCoachInfo coachInfo = tCoachInfoMapper.selectOne(new QueryWrapper<TCoachInfo>().eq("coach_id", coachId));
        if (coachInfo==null) throw new AccountException(NOT_FIND_THIS_USER);
        CoachInfoDto dto = BeanUtil.toBean(coachInfo,CoachInfoDto.class);
        return dto;
    }

    @Override
    public String coachRealNameAuthentication(RealNameAuthenticationForm form) throws Exception {
        String str = JwtUtil.realNameAuthentication(form.getCardId(), form.getRealName());
        if (!str.equals("true")) return "实名认证失败，请重试";
        Integer coachId = JwtUtil.getUserId(3);
        if (coachId==null) throw new AccountException(AccountExceptionCode.USER_NOT_LOGIN);
        TCoachInfo coach = getOne(new QueryWrapper<TCoachInfo>().eq("coach_id", coachId));
        if (coach!=null) {
            TCoach tCoach = tCoachMapper.selectById(coach.getCoachId());
            tCoach.setCoachState(1);
            coach.setCoachRealname(form.getRealName());
            coach.setCoachIdnum(form.getCardId());
            coach.setCoachPhoto(form.getCardPhoto());
            Long age = JwtUtil.getAgeForIdcard(form.getCardId());
            String gender = JwtUtil.getSexFromIdCard(form.getCardId());
            coach.setCoachAge(age.intValue());
//            coach.setCoachGender(gender);
            if(gender.equals("女")){
                coach.setCoachGender(2);
            }else {
                coach.setCoachGender(1);
            }
            updateById(coach);
        }else{
//            TTeacherInfo tTeacherInfo = new TTeacherInfo();
//            tTeacherInfo.setRealName(form.getRealName());
//            tTeacherInfo.setCardId(form.getCardId());
//            tTeacherInfo.setCardPhoto(form.getCardPhoto());
//            Long age = JwtUtil.getAgeForIdcard(form.getCardId());
//            String gender = JwtUtil.getSexFromIdCard(form.getCardId());
//            tTeacherInfo.setCoachAge(age);
//            tTeacherInfo.setCoachGender(gender);
//            save(tTeacherInfo);
            return "请先注册";
        }
        return "实名认证通过";
    }

    @Override
    public TCoachInfo coachInfoPerfect(CoachInfoPerfectForm form) throws Exception {
        Integer coachId = JwtUtil.getUserId(3);
        if (coachId==null) throw new AccountException(NOT_FIND_THIS_USER);
        TCoachInfo coach = getOne(new QueryWrapper<TCoachInfo>().eq("coach_id", coachId));
        coach.setCoachAuthen(form.getCoachAuthen());
        coach.setCoachDetails(form.getCoachDetails());
        coach.setCoachPhone(form.getCoachPhone());
        coach.setCoachMoney(form.getCoachMoney());
        coach.setWantJob(form.getWantJob());
        coach.setCoachGood(form.getCoachGood());
        coach.setResumeState(1);
        tCoachInfoMapper.updateById(coach);
        return coach;
    }

    @Override
    public String setCoachInfoState() throws Exception {
        Integer coachId = JwtUtil.getUserId(3);
        if(coachId==null) throw new AccountException(AccountExceptionCode.USER_NOT_LOGIN);
        TCoachInfo tCoachInfo = tCoachInfoMapper.selectOne(new QueryWrapper<TCoachInfo>().eq("coach_id", coachId));
        if(tCoachInfo==null)throw new AccountException(NOT_FIND_THIS_USER);
        if(tCoachInfo.getResumeState()==1){
            tCoachInfo.setResumeState(0);
            tCoachInfoMapper.updateById(tCoachInfo);
            return "简历隐藏成功";
        }else {
            tCoachInfo.setResumeState(1);
            tCoachInfoMapper.updateById(tCoachInfo);
            return "简历展示成功";
        }
    }

}
